/**
 * 转成树型结构
 * @param list
 * @param parentId
 * @param idKey
 * @param parentIdKey
 * @param childrenKey
 */
export const toTree = (list: any[], parentId: number | string | null, idKey: string = 'id',
                       parentIdKey: string = 'parentId', childrenKey: string = 'children') => {
  const len = list.length
  const loop = (parId: number | string | null) => {
    const res = []
    for (let i = 0; i < len; i++) {
      const item = list[i]
      if (item[parentIdKey] === parId) {
        item[childrenKey] = loop(item[idKey])
        res.push(item)
      }
    }
    return res
  }
  return loop(parentId)
}

export const download = (url: string, fileName: string = '下载文件') => {
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
}
